package cz.jm.web.manhunter.client.authentication;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import cz.jm.web.manhunter.client.services.UserDataServiceAsync;
import cz.jm.web.manhunter.shared.User;
import cz.jm.web.manhunter.shared.User.UserState;


public class LoginPresenter extends cz.jm.web.jmlib.client.dialog.login.LoginPresenter {

	private UserDataServiceAsync userDataService;

	public LoginPresenter(UserDataServiceAsync service, Display display) {
	    super(display);
	    userDataService = service;
    }

	
	@Override
    public void login(String userName, String password) {
		userDataService.authenticateUser(userName, password, new AsyncCallback<User>() {

			@Override
			public void onSuccess(User result) {
				unblockApplication();

				if (result == null) {
					onLoginFail(User.UserState.ERROR);
				}
				else if (result.isValid()) {
					onLoginSuccess(result);
				}
				else {
					onLoginFail(result.getState());
				}
			}

				@Override
			public void onFailure(Throwable caught) {
				unblockApplication();
				onLoginFail(User.UserState.ERROR);
				// TODO osetrit vynimku RPC						
			}
		});											

	    
    }	
	
	public void onLoginSuccess(User user) {
		// TODO poznacit ako prihlaseneho usera
		Window.alert("onLoginSuccess " + user.getUsername());
	}
	public void onLoginFail(UserState state) {
		// TODO vizualizacia nepodareneho prihasenia ...
		Window.alert("onLoginFail " + state.toString());
	}
}
